var appInstance = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

    pindan_panel:Object,
  },

  /**
   * 组件的初始数据
   */
  data: {
    show:0,
    pindan_list:[],
  },

  /**
   * 组件的方法列表
   */
  methods: {

    close:function( event ){

      this.setData({
        show:0
      });
    },

    get_pindan_list:function(){

      var t = this;
      var url = 'checkout.order.pindan_list&goods_id=' + this.data.pindan_panel.product_id;

      appInstance.get(url, {}, res=>{
        t.setData({
          pindan_list:res.data
        }, function(){

          t.daojishi();
        });


      });

    },

    daojishi:function(){

      var t = this;

      var interval = setInterval(function(){

        var now = new Date().getTime();

        t.data.pindan_list.forEach(function( item ){
          var start_time = new Date( item.start_time.replace('-','/') ).getTime();
          var end_time = start_time + item.validate_time*3.6e6;
          var cha = end_time - now;
          if( cha <= 0 ){

            clearInterval( interval );
            t.get_pindan_list();
          }
          var days = Math.floor(cha / (24*3600*1000) );

          //计算出小时数
          var leave1=cha % (24*3600*1000)    //计算天数后剩余的毫秒数
          var hours=Math.floor(leave1/(3600*1000))
          hours = hours<10?'0'+hours:hour;
          //计算相差分钟数

          var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
          var minutes=Math.floor(leave2/(60*1000))
          minutes = minutes<10?'0'+minutes:minutes;
          //计算相差秒数

          var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
          var seconds=Math.round(leave3/1000)
          seconds = seconds<10?'0'+seconds:seconds;

          item.remaining = days ?  days + '天':'';
          item.remaining += hours + ':' + minutes + ':' + seconds;

        });

        t.setData({
          pindan_list:t.data.pindan_list
        });

      },1000);
    },

    buy_pindan:function( e ){

      e.currentTarget.dataset.order_id;

      this.close();

      var myEventDetail = {
        order_id:e.currentTarget.dataset.order_id
      };
      var myEventOption = {
      };
      this.triggerEvent('buy_pindan_list', myEventDetail, myEventOption);



    }
  },

  observers:{


    'pindan_panel.notice':function( after ){

      // 第一次不调用
      if( !after ){
        return;
      }

      this.setData({
        show:1
      });

      this.get_pindan_list();

    }
  },
})
